/*----------------------------------------
  
  Live
  
  放送一覧クラス
  
----------------------------------------*/


/*
  Class
  
*/

function Live( xml ) {
	
	this.liveInfo = new Array(); // 放送の基本情報の配列
	this.onAir = true; //放送中か否かのステータス
	this.lastCheckDate = new Date(); //最後に放送を確認した日時
	
	this.resolveXML(xml); // XMLを分解、liveInfoにデータ格納
	this.exportLiveDataToHTML();
	
	return this;
}


/*----------------------------------------
  header
----------------------------------------*/
Live.prototype.resolveXML = Live_resolveXML;
Live.prototype.exportLiveDataToHTML = Live_exportLiveDataToHTML;
Live.prototype.formatAtTimer = Live_formatAtTimer;
Live.prototype.formatDate = Live_formatDate;
Live.prototype.infoUpdate = Live_infoUpdate;

/*----------------------------------------
  method
----------------------------------------*/


/*
  exportLiveDataToHTML
  HTMLの生成、挿入、表示を行う
*/

function Live_exportLiveDataToHTML (){
	
	var live = this;
	
	var row = $('<div id="'+this.liveInfo.guid+'" class="article">');
    $(row).click(function(){
		widget.openURL(live.liveInfo.link);
	})
	
	var thumbnail = $('<div class="thumbnail"></div>');
	
	if ( this.liveInfo.totsu || this.liveInfo.face ){
		var mark = $( '<div class="mark"></div>' );
		if ( this.liveInfo.totsu ){
			$(mark).append('<img src="images_n2lc/icon_totsu.png">');
		}
		if ( this.liveInfo.face ){
			$(mark).append('<img src="images_n2lc/icon_face.png">');
		}
		$(thumbnail).append(mark);
	}
	$(thumbnail).css('background-image','url(http://icon.nicovideo.jp/'+this.liveInfo.type+'/'+this.liveInfo.community_id+'?)');
	$(row).append(thumbnail);
	
	var anchor = $('<div class="articlehead">');
	$(anchor).append('<span class="owner_name">'+this.liveInfo.owner_name+'</span>');
	$(anchor).append('<span class="time">'+this.formatAtTimer()+'</span>');
	$(anchor).append('<div class="title">'+this.liveInfo.title+'</div>');
	$(anchor).append('<div class="description">'+this.liveInfo.description+'</div>');
	$(anchor).append('<span class="category">'+this.liveInfo.category+'</span>');
	$(anchor).append('<span class="date">'+this.formatDate()+'</span>');
	
	$(row).append(anchor);
	
	/* HTML挿入処理 */
    if( $('#'+this.liveInfo.guid).length == 1 ){
		$('#'+this.liveInfo.guid).replaceWith(row);
	}else if( $('#FarstMSG').length == 1 ){
		$('#FarstMSG').remove();
        $(row).appendTo('#content').hide();
	}else{
		
		var date = this.liveInfo.pubdate;
		var viewd = 0;
		
		$('#content > div').each(function(){
			var nowid = $(this).attr('id');
			var nowdate = Lives[nowid].liveInfo.pubdate;
			
			if ( date > nowdate ){
				$('#'+nowid).before(row).hide();
				viewd = 1;
				return false;
			}
			
		});
		
		if ( viewd == 0 ){
			$(row).appendTo('#content').hide();
		}
		
	}
    
    $('#'+this.liveInfo.guid).slideDown('fast',function(){
        refreshScrollArea();
    });
	
}


/*
  resolveXML
  引数のXMLデータを分解し、liveInfoプロパティに格納
*/

function Live_resolveXML (xml){
	/*
		this.liveInfo.guid = lvID
		this.liveInfo.title = 放送タイトル
		this.liveInfo.link = リンク先
		this.liveInfo.pubdate = date形式 開始日時
		this.liveInfo.enddate = date形式 終了予定日時
		this.liveInfo.description = 放送説明文
		this.liveInfo.category = カテゴリー
		this.liveInfo.community_id = コミュニティ/チャンネルID
		this.liveInfo.community_name = コミュニティ/チャンネルID
		this.liveInfo.type = community／channel
		this.liveInfo.owner_name = 配信者名
		this.liveInfo.totsu = 凸フラグ
		this.liveInfo.face = 顔出しフラグ
	*/
	
	this.liveInfo.community_id   = $(xml).find("community_id").text();
	
	this.liveInfo.guid           = $(xml).find("guid").text();
	this.liveInfo.title          = $(xml).find("title").text();
	this.liveInfo.link	         = $(xml).find("link").text();
	this.liveInfo.pubdate        = new Date( $(xml).find("pubDate").text().replace(/^.*?, (\d+) (.+) (\d+) (\d+:\d+:\d+) .*?$/, "$2 $1, $3 $4") );
	this.liveInfo.enddate        = new Date( this.liveInfo.pubdate.getTime() + 1800000 );
	this.liveInfo.description    = $(xml).find("description").text();
	this.liveInfo.category       = $(xml).find("category:eq(0)").text();
	this.liveInfo.community_name = $(xml).find("community_name").text();
	this.liveInfo.type           = $(xml).find("type").text();
	this.liveInfo.owner_name     = $(xml).find("owner_name").text();
	
	var categoryAll = $(xml).find("category").text();
	(categoryAll.match(/凸待ち/) != null )?this.liveInfo.totsu = true:this.liveInfo.totsu = false;
	(categoryAll.match(/顔出し/) != null )?this.liveInfo.face = true:this.liveInfo.face = false;
	
}


/*
  formatAtTimer
  残りの放送時間を返す 
*/

function Live_formatAtTimer(){
	
	this.infoUpdate();
	
	var nowSec = new Date().getTime();
	
	var atTimeSec = this.liveInfo.enddate.getTime() - nowSec;
	
	var m = Math.floor(atTimeSec / 60000 )
	var s = Math.floor((atTimeSec - m * 60000)/1000);
	
	if (m<10 && m>=0){m="0"+m;}
	if (s<10 && s>=0){s="0"+s;}
	
	console.log ( 'atTimeSec:'+atTimeSec );
	console.log ( 'atTimeSec:'+atTimeSec );
	console.log ( 'atTimeSec:'+atTimeSec );
	
	return "@"+m+":"+s
}

/*
  infoUpdate
  枠数や終了時刻などの情報を更新
*/
function Live_infoUpdate(){
	
	last = this.lastCheckDate.getTime();
	pub = this.liveInfo.pubdate.getTime();
	
	this.liveInfo.plaingTime = last - pub;
	this.liveInfo.waku = Math.ceil( this.liveInfo.plaingTime/1800000 );
	this.liveInfo.enddate = new Date( this.liveInfo.waku * 1800000 + pub );
	
}


/*
  formatDate
  時刻を返す
*/

function Live_formatDate(){
	
	var h = this.liveInfo.pubdate.getHours();
	var m = this.liveInfo.pubdate.getMinutes();
	var s = this.liveInfo.pubdate.getMinutes();
	
	if (h<10 && h>=0){h="0"+h;}
	if (m<10 && m>=0){m="0"+m;}
	if (s<10 && s>=0){s="0"+s;}
	
	return h+':'+m+':'+s;
	
}